#include  "employee.h"
#include  <string.h>

#define  UNKNOWN "???"

std::ostream& operator<<( std::ostream& os, const Employee& employee)
{
    return employee.print( os);
}

Employee::Employee( const char* desc)
    :ID( nextID++), description( copyStr( desc))
{ }

Employee::Employee( )
    :ID(-1), description( copyStr( UNKNOWN))
{ }

Employee::Employee( const Employee& employee)
    :ID( employee.ID), description( copyStr( employee.getDescription( )))
{
}

void Employee::setDescription( const char *src)
{
    char *tmp = copyStr( src);
    delete[] this->description;
    this->description = tmp;
}

char* Employee::copyStr( const char* desc)
{
    char* tmp = NULL;
    if ( NULL != desc)
    {
        tmp = new char[ strlen( desc) + 1];
        strcpy( tmp, desc);
    }
    else
    {
        tmp = new char[ strlen( UNKNOWN) + 1];
        strcpy( tmp, UNKNOWN);
    }

    return tmp;
}

int Employee::getID( ) const
{
    return this->ID;
}

const char* const Employee::getDescription( ) const
{
    return this->description;
}

Employee& Employee::operator=( const Employee& employee)
{
    std::cout << "operator=" << std::endl;
    this->setDescription( employee.getDescription( ));
    this->ID = employee.getID( );
    return *this;
}

std::ostream& Employee::print( std::ostream& os) const
{
    return os << "Employee ID = " << getID() 
              << std::endl;
}

bool Employee::operator==( const Employee& employee) const
{
    return ( this->getID( ) == employee.getID( ));
}

bool Employee::operator!=( const Employee& employee) const
{
    return !( *this == employee);
}

bool Employee::operator==( int id) const
{
    std::cout << "operator==( int id)" << std::endl;
    return ( this->getID( ) == id);
}

void Employee::setID( int id)
{
    this->ID = id;
}

Employee::~Employee( )
{
    delete[] description;
}
